package com.zdltech.lightappupdate;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.zdltech.lightappupdatelib.LightUpdateManager;
import com.zdltech.lightappupdatelib.core.AppVersionConvertCallback;
import com.zdltech.lightappupdatelib.core.UpdateCallback;
import com.zdltech.lightappupdatelib.entity.AppVertionEntity;
import com.zdltech.lightappupdatelib.utils.LogUtils;

import java.util.HashMap;
import java.util.Map;

public class MainDemoActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_demo);
    }

    public void update(View view){
        //GET 请求
        //设置请求的地址
        LightUpdateManager.mUpdateUrl = "http://192.168.0.251:8080/update/app/getVersion";
        //设置请求的参数
        Map<String,String> params = new HashMap<>();
        LightUpdateManager.checkNewApp(this, new UpdateCallback() {
            @Override
            public void start() {
                LogUtils.e("UpdateCallback start is run");
            }

            @Override
            public void end() {
                LogUtils.e("UpdateCallback end is run");
            }

            @Override
            public void update(AppVertionEntity appVertionEntity) {
                LogUtils.e("UpdateCallback update is run");
                LightUpdateManager.showDialog(MainDemoActivity.this,appVertionEntity);
            }
            @Override
            public void error(int code, String error) {
                LogUtils.e("UpdateCallback error is run error="+error);
            }
        },false,params);
    }

    public void request(View view){
        //POST请求
        LightUpdateManager.mUpdateUrl = "http://192.168.0.251:8080/update/app/getVersion";
        Map<String,String> params = new HashMap<>();
        LightUpdateManager.checkNewApp(this, new UpdateCallback() {
            @Override
            public void start() {
                LogUtils.e("UpdateCallback start is run");
            }

            @Override
            public void end() {
                LogUtils.e("UpdateCallback end is run");
            }

            @Override
            public void update(AppVertionEntity appVertionEntity) {
                LogUtils.e("UpdateCallback update is run");
                LightUpdateManager.showDialog(MainDemoActivity.this,appVertionEntity);
            }

            @Override
            public void error(int code, String error) {
                LogUtils.e("UpdateCallback error is run error="+error);
            }
        },true,params);

    }

    public void myDefine(View view){//自定义请求数据 转换 库识别的方式，只需要实现 APP VersionConvertCallback就可
        LightUpdateManager.mUpdateUrl = "http://192.168.0.251:8080/update/app/getVersion";
        Map<String,String> params = new HashMap<>();
        LightUpdateManager.checkNewApp(this, new UpdateCallback() {
            @Override
            public void start() {
                LogUtils.e("UpdateCallback start is run");//加载对话框
            }

            @Override
            public void end() {
                LogUtils.e("UpdateCallback end is run");//隐藏加载对话框
            }

            @Override
            public void update(AppVertionEntity appVertionEntity) {//处理执行结果
                LogUtils.e("UpdateCallback update is run");
                LightUpdateManager.showDialog(MainDemoActivity.this, appVertionEntity);
            }

            @Override
            public void error(int code, String error) {
                LogUtils.e("UpdateCallback error is run error=" + error);//错误处理
            }
        }, true, params, new AppVersionConvertCallback() {
            @Override
            public AppVertionEntity convert(String result) {
                LogUtils.e("convert is result="+result);
                LogUtils.e("convert 自定义数据对象到 系统识别对象 AppVertionEntity");
                AppVertionEntity appVertionEntity = new  AppVertionEntity();
                appVertionEntity.setUpdateInfo("convert 自定义数据对象到 系统识别对象");
                appVertionEntity.setUpdateType("web");
                appVertionEntity.setUpdateUrl("http:www.zdltech.com");
                return appVertionEntity;
            }
        });

        //LightAppUpdateUtils.saveIgnoreVersion(getActivity(), mUpdateApp.getVersion()+"");
        //如果忽略了，默认是不能显示弹出框的 需要设置忽略版本为空，如果还有其他业务，用户可以自行设置，例如自己多保存一份 更新数据到本地
        //LightAppUpdateUtils.saveIgnoreVersion(getActivity(), "");
    }
}
